<?php
namespace Api\Model;



class MedicalRecordDetailModel extends BaseModel {

    private static $instance;

    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    public function getListByRecordId($record_id){
        $where['record_id']=$record_id;
        $where['is_show']=1;
        $rows = $this->where($where)->select();
        $rows || $rows=[];
        foreach($rows as $key=>$val){
            $rows[$key]['create_time']=date('Y-m-d',strtotime($val['create_time']));
        }
        return $rows;
    }

    public function getOneById($id){
        $where['id']=$id;
        $row = $this->where($where)->find();
        return $row;
    }

    public function addOrUpdate($record_id,$id,$data){
        $rst=array();
        if($id>0){
            $row = $this->getOneById($id);
            if($row['record_id']==$record_id){
                $info=array();
                $info['r_apparent_optometry_ds']=$data['r_apparent_optometry_ds'];
                $info['r_apparent_optometry_dc']=$data['r_apparent_optometry_dc'];
                $info['r_apparent_optometry_degree']=$data['r_apparent_optometry_degree'];
                $info['r_apparent_optometry_equals']=$data['r_apparent_optometry_equals'];
                $info['r_cycloplegic_refraction_ds']=$data['r_cycloplegic_refraction_ds'];
                $info['r_cycloplegic_refraction_dc']=$data['r_cycloplegic_refraction_dc'];
                $info['r_cycloplegic_refraction_degree']=$data['r_cycloplegic_refraction_degree'];
                $info['r_cycloplegic_refraction_equals']=$data['r_cycloplegic_refraction_equals'];
                $info['r_intraocular_pressure']=$data['r_intraocular_pressure'];
                $info['r_corneal_diameter']=$data['r_corneal_diameter'];
                $info['r_thin_thickness']=$data['r_thin_thickness'];
                $info['r_max_curvature']=$data['r_max_curvature'];
                $info['l_apparent_optometry_ds']=$data['l_apparent_optometry_ds'];
                $info['l_apparent_optometry_dc']=$data['l_apparent_optometry_dc'];
                $info['l_apparent_optometry_degree']=$data['l_apparent_optometry_degree'];
                $info['l_apparent_optometry_equals']=$data['l_apparent_optometry_equals'];
                $info['l_cycloplegic_refraction_ds']=$data['l_cycloplegic_refraction_ds'];
                $info['l_cycloplegic_refraction_dc']=$data['l_cycloplegic_refraction_dc'];
                $info['l_cycloplegic_refraction_degree']=$data['l_cycloplegic_refraction_degree'];
                $info['l_cycloplegic_refraction_equals']=$data['l_cycloplegic_refraction_equals'];
                $info['l_intraocular_pressure']=$data['l_intraocular_pressure'];
                $info['l_corneal_diameter']=$data['l_corneal_diameter'];
                $info['l_thin_thickness']=$data['l_thin_thickness'];
                $info['l_max_curvature']=$data['l_max_curvature'];

                $info['r_yunyi']=$data['r_yunyi'];
                $info['r_yunyi_location']=$data['r_yunyi_location'];
                $info['l_yunyi']=$data['l_yunyi'];
                $info['l_yunyi_location']=$data['l_yunyi_location'];

                $res=$this->where(['id'=>$id])->save($info);
                if ($res !== FALSE) {
                    $rst['result']=200;
                    $rst['msg']='修改成功';
                }else{
                    $rst['result']=201;
                    $rst['msg']='修改失败';
                }
            }else{
                $rst['result']=202;
                $rst['msg']='record_id有误';
            }
        }else{
            $info=array();
            $info['record_id']=$record_id;
            $info['r_apparent_optometry_ds']=$data['r_apparent_optometry_ds'];
            $info['r_apparent_optometry_dc']=$data['r_apparent_optometry_dc'];
            $info['r_apparent_optometry_degree']=$data['r_apparent_optometry_degree'];
            $info['r_apparent_optometry_equals']=$data['r_apparent_optometry_equals'];
            $info['r_cycloplegic_refraction_ds']=$data['r_cycloplegic_refraction_ds'];
            $info['r_cycloplegic_refraction_dc']=$data['r_cycloplegic_refraction_dc'];
            $info['r_cycloplegic_refraction_degree']=$data['r_cycloplegic_refraction_degree'];
            $info['r_cycloplegic_refraction_equals']=$data['r_cycloplegic_refraction_equals'];
            $info['r_intraocular_pressure']=$data['r_intraocular_pressure'];
            $info['r_corneal_diameter']=$data['r_corneal_diameter'];
            $info['r_thin_thickness']=$data['r_thin_thickness'];
            $info['r_max_curvature']=$data['r_max_curvature'];
            $info['l_apparent_optometry_ds']=$data['l_apparent_optometry_ds'];
            $info['l_apparent_optometry_dc']=$data['l_apparent_optometry_dc'];
            $info['l_apparent_optometry_degree']=$data['l_apparent_optometry_degree'];
            $info['l_apparent_optometry_equals']=$data['l_apparent_optometry_equals'];
            $info['l_cycloplegic_refraction_ds']=$data['l_cycloplegic_refraction_ds'];
            $info['l_cycloplegic_refraction_dc']=$data['l_cycloplegic_refraction_dc'];
            $info['l_cycloplegic_refraction_degree']=$data['l_cycloplegic_refraction_degree'];
            $info['l_cycloplegic_refraction_equals']=$data['l_cycloplegic_refraction_equals'];
            $info['l_intraocular_pressure']=$data['l_intraocular_pressure'];
            $info['l_corneal_diameter']=$data['l_corneal_diameter'];
            $info['l_thin_thickness']=$data['l_thin_thickness'];
            $info['l_max_curvature']=$data['l_max_curvature'];
            $info['create_time']=date('Y-m-d H:i:s');

            $info['r_yunyi']=$data['r_yunyi'];
            $info['r_yunyi_location']=$data['r_yunyi_location'];
            $info['l_yunyi']=$data['l_yunyi'];
            $info['l_yunyi_location']=$data['l_yunyi_location'];
            $res = $this->add($info);
            if($res){
                $rst['result']=200;
                $rst['id']=$res;
                $rst['msg']='新增成功';
            }else{
                $rst['result']=201;
                $rst['msg']='新增失败';
            }
        }

        return $rst;

    }

    //更新病历图片
    public function updateImg($id,$data){

            $info = array();
            $info['check_time']=$data['check_time'] ==null  ? '' : $data['check_time'] ;
            $info['r_overview_img']=$data['r_overview_img']  ==null  ? '' : $data['r_overview_img'];
            $info['r_bad_img']=$data['r_bad_img']  ==null  ? '' : $data['r_bad_img'];
            $info['r_four_map_img']=$data['r_four_map_img']  ==null  ? '' : $data['r_four_map_img'];
            $info['r_topometric_img']=$data['r_topometric_img']  ==null  ? '' :$data['r_topometric_img'];
            $info['r_corvis_img']=$data['r_corvis_img']  ==null  ? '' :$data['r_corvis_img'];
            $info['l_overview_img']=$data['l_overview_img'] ==null  ? '' :$data['l_overview_img'];
            $info['l_bad_img']=$data['l_bad_img']  ==null  ? '' :$data['l_bad_img'];
            $info['l_four_map_img']=$data['l_four_map_img']  ==null  ? '' :$data['l_four_map_img'];
            $info['l_topometric_img']=$data['l_topometric_img']  ==null  ? '' :$data['l_topometric_img'];
            $info['l_corvis_img']=$data['l_corvis_img']  ==null  ? '' :$data['l_corvis_img'];
            $info['contrast_img']=$data['contrast_img']  ==null  ? '' :$data['contrast_img'];
            $info['status']=1;
            $res=  $this->where(['id'=>$id])->save($info);
            return $res;

	}

    //更新病历图片
    public function getImage($id){
        $fields='id,check_time,';
        $fields.='r_overview_img,r_bad_img,r_four_map_img,r_topometric_img,r_corvis_img,';
        $fields.='l_overview_img,l_bad_img,l_four_map_img,l_topometric_img,l_corvis_img,';
        $fields.='contrast_img';
        $res=  $this->where(['id'=>$id])->field($fields)->find();
        return $res;

    }

    public function deleteRecordDetail($id){
        $where['id']=$id;
        $res= $this->where($where)->save(['is_show'=>0]);
        if(false!==$res){
            $rst['result']=200;
            $rst['msg']='删除成功';
        }else{
            $rst['result']=201;
            $rst['msg']='删除失败';
        }
        return $rst;
    }


    //获取病历是否有完整病历
    public function canShare($record_id){
        $where['record_id']=$record_id;
        $where['is_show']=1;
        $where['status']=1;
        $whereOr['r_bad_img'] = array('neq', '');
        $whereOr['r_four_map_img'] = array('neq', '');
        $whereOr['l_bad_img'] = array('neq', '');
        $whereOr['l_four_map_img'] = array('neq', '');
        $whereOr['_logic'] = 'or';
        $where['_complex'] = $whereOr;
        $rows=$this->where($where)->select();
        if($rows){
            return 1;
        }else{
            return 0;
        }
    }
}